package com.yuwen.yuwenRPC.server;

import io.vertx.core.Vertx;

/**
 * @ClassNameVertxHttpServer
 * @Description TODO
 * @Author钰玟
 * @Date2025/3/29 下午8:12
 * @Version 1.0
 **/

public class VertxHttpServer implements  HttpServer{
    @Override
    public void doStart(int port) {
        // 1.创建 vertx 实例
        Vertx vertx = Vertx.vertx();

        // 2. 创建http服务器
        io.vertx.core.http.HttpServer server = vertx.createHttpServer();

        // 3. 监听端口号
//        server.requestHandler(request -> {
//            //3.1 处理http请求
//            System.out.println("Received request: " + request.method()
//            + " " + request.uri());
//
//            // 3.2 发送http响应
//            request.response()
//                    .putHeader("content-type", "text/plain")
//                    .end("Hello from Vert.x HTTP Server");
//        });
        //3. 监听端口 并请求处理
        server.requestHandler(new HttpServerHandler());

        // 4. 启动Http 服务器并监听指定端口
        server.listen(port,result->{
            if(result.succeeded()){
                System.out.println("Vert.x HTTP Server started on port " + port);
            }
            else {
                System.err.println("Failed to start Vert.x HTTP Server" + result.cause());
            }
        });
    }
}
